//
//  CVCellOfTestCollect.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/21.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "CVCellOfTestCollect.h"
#import "BaseTableView.h"
#import "MJMacro.h"
//// 收藏文章的 tableViewCell
//#import "TVCellOfReadAriticle.h"
// 收藏测试 tableViewCell
#import "TVCellOfBelowInformationDoTest.h"
#import "ZWYNetTool.h"
// SDImageView
#import "UIImageView+WebCache.h"
// 收藏测试的 model
#import "DataOfTestCollect.h"
#import "FavoriteOfDataTestCollect.h"
#import "RootClassOfTestCollect.h"
#import "MJRefresh.h"
// 要跳到的界面
#import "VCOfBeginDoTest.h"
// 当前所在的 VC
#import "VCOfCollec.h"
@interface CVCellOfTestCollect ()<UITableViewDelegate, UITableViewDataSource>
// 收藏的测试题
@property (nonatomic, strong) RootClassOfTestCollect *rootClassOfTestCollect;
// 收藏的测试
@property (nonatomic, strong) BaseTableView *tableViewOfCollect;
// 收藏测试的赋值的数组
@property (nonatomic, strong) NSMutableArray<FavoriteOfDataTestCollect *>* mArrayOfDataOfTestCollect;
@end
@implementation CVCellOfTestCollect
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        [self handleDataTestCollect];
    }
    return self;
}
#pragma mark - 收藏测试题的, 数据解析
- (void)handleDataTestCollect{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/user-test-list.json?key=18ee0593a01c07f71d28fa936d671457&_platform=ios&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"%@", result);
        _rootClassOfTestCollect = [RootClassOfTestCollect modelDic:result];
        _mArrayOfDataOfTestCollect = _rootClassOfTestCollect.data.favorite.mutableCopy;
        NSLog(@"%@", _rootClassOfTestCollect.data.favorite[1].result_title);
        [self.tableViewOfCollect reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableViewOfCollect = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.contentView addSubview:_tableViewOfCollect];
    self.tableViewOfCollect.delegate = self;
    self.tableViewOfCollect.dataSource = self;
    [self.tableViewOfCollect registerNib:[UINib nibWithNibName:@"TVCellOfBelowInformationDoTest" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfBelowInformationDoTest"];
    self.tableViewOfCollect.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
#pragma mark - 下拉刷新
- (void)loadMoreData{
    static int i = 0;
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/user-test-list.json?key=18ee0593a01c07f71d28fa936d671457&_platform=ios&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSLog(@"%@", result);
        _rootClassOfTestCollect = [RootClassOfTestCollect modelDic:result];
        for (FavoriteOfDataTestCollect *favoriteOfDataTestCollect in _rootClassOfTestCollect.data.favorite) {
            [_mArrayOfDataOfTestCollect addObject:favoriteOfDataTestCollect];
        }
        [self.tableViewOfCollect reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    [self.tableViewOfCollect.mj_footer endRefreshing];
    i++;
}
#pragma mark - tabelView 代理方法 row 的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mArrayOfDataOfTestCollect.count;
}
#pragma mark - 返回每个 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 6;
}
#pragma mark - 分区头的文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"   一共%@个测试题, 来试试吧", _rootClassOfTestCollect.data.num];
}
#pragma mark - TableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfBelowInformationDoTest *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfBelowInformationDoTest" forIndexPath:indexPath];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_mArrayOfDataOfTestCollect[indexPath.row].test_cover] placeholderImage:[UIImage imageNamed:@"0"]];
    cell.titleLabel.text = _mArrayOfDataOfTestCollect[indexPath.row].test_title;
    cell.commentnumLabel.text = _mArrayOfDataOfTestCollect[indexPath.row].test_commentnum;
    cell.viewnumLabel.text = _mArrayOfDataOfTestCollect[indexPath.row].test_viewnum;
    return cell;
}
#pragma mark - tableView 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfBeginDoTest *vcOfBeginDoTest = [[VCOfBeginDoTest alloc] init];
    vcOfBeginDoTest.coverString = _mArrayOfDataOfTestCollect[indexPath.row].test_cover;
    vcOfBeginDoTest.titleString = _mArrayOfDataOfTestCollect[indexPath.row].test_title;
    vcOfBeginDoTest.contentString = _mArrayOfDataOfTestCollect[indexPath.row].result_content;
    vcOfBeginDoTest.viewnumString = _mArrayOfDataOfTestCollect[indexPath.row].test_viewnum;
    vcOfBeginDoTest.myID = _mArrayOfDataOfTestCollect[indexPath.row].test_id;
    VCOfCollec *vcOfCollec = (VCOfCollec *)self.nextResponder.nextResponder.nextResponder;
    [vcOfCollec presentViewController:vcOfBeginDoTest animated:YES completion:^{
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
